package com.jrtc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.entity.bo.*;
import com.jrtc.base.entity.vo.PatientRecordcaseVo;
import com.jrtc.base.entity.vo.RecordCasVo;
import com.jrtc.base.entity.vo.RecordCaseBigVo;
import com.jrtc.base.entity.vo.RecordCaseMiddleVo;
import com.jrtc.dao.*;
import com.jrtc.service.PatientRecordcaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 患者病历表(PatientRecordcase)表服务实现类
 *
 * @author makejava
 * @since 2022-03-21 17:34:12
 */
@Service("patientRecordcaseService")
public class PatientRecordcaseServiceImpl implements PatientRecordcaseService
{
    @Resource
    private PatientRecordcaseDao patientRecordcaseDao;

    @Resource
    private PatientDao patientDao;

    @Resource
    private PatientRecordCaseMainCategoryDao patientRecordCaseMainCategoryDao;

    @Resource
    private PatientRecordCaseMiddleClassDao patientRecordCaseMiddleClassDao;

    @Resource
    private PatientMedicalRecordsDao patientMedicalRecordsDao;

    @Resource
    private DoctorDao doctorDao;

    /**
     * 通过ID查询单条数据
     *
     * @param recordcaseId 主键
     * @return 实例对象
     */
    @Override
    public PatientRecordcase queryById(Integer recordcaseId) {
        return this.patientRecordcaseDao.queryById(recordcaseId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<PatientRecordcase> queryAllByLimit(int offset, int limit) {
        return this.patientRecordcaseDao.queryAllByLimit(offset, limit);
    }

    @Override
    public IPage<PatientRecordcase> queryPatientRecordcaseList(PatientRecordcase patientRecordcase)
    {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<PatientRecordcase> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(patientRecordcase.getPageNo(), patientRecordcase.getPageSize());
        IPage<PatientRecordcase> patientRecordcasePage = this.patientRecordcaseDao.queryPatientRecordcaseList(page, patientRecordcase);
        return patientRecordcasePage;
    }
    /**
     * 新增数据
     *
     * @param patientRecordcase 实例对象
     * @return 实例对象
     */
    @Override
    public PatientRecordcase insert(PatientRecordcase patientRecordcase) {
        this.patientRecordcaseDao.insert(patientRecordcase);
        return patientRecordcase;
    }

    /**
     * 修改数据
     *
     * @param patientRecordcase 实例对象
     * @return 实例对象
     */
    @Override
    public PatientRecordcase update(PatientRecordcase patientRecordcase) {
        this.patientRecordcaseDao.update(patientRecordcase);
        return this.queryById(patientRecordcase.getRecordcaseId());
    }

    /**
     * 通过主键删除数据
     *
     * @param recordcaseId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer recordcaseId) {
        return this.patientRecordcaseDao.deleteById(recordcaseId) > 0;
    }

    /**
     * 创建病例
     * */
    @Transactional
    @Override
    public int createCase(PatientRecordcaseVo patientRecordcaseVo) throws Exception {
        int i = 0;
        if (patientRecordcaseVo.getPatientId() == 0){
            throw new Exception("患者id不能为空");
        }
        Patient patient = patientDao.patientByPatientId(patientRecordcaseVo.getPatientId());
        if (patient == null){
            throw new Exception("未查到患者信息");
        }
        //添加患者病例信息
        PatientRecordcase patientRecordcase = new PatientRecordcase();
        patientRecordcase.setPatientId(patientRecordcaseVo.getPatientId());
        patientRecordcase.setPatientName(patient.getPatientName());
        int aCase = patientRecordcaseDao.createCase(patientRecordcase);
        if (aCase <= 0){
            throw new Exception("新增患者病例失败");
        }
        //添加病例大分类信息
        if (patientRecordcaseVo.getRecordCaseBigVoList() == null || patientRecordcaseVo.getRecordCaseBigVoList().size() <= 0){
            throw new Exception("病例大分类不能为空");
        }
        for (RecordCaseBigVo recordCaseBigVo : patientRecordcaseVo.getRecordCaseBigVoList()) {
            PatientRecordCaseMainCategory patientRecordCaseMainCategory = new PatientRecordCaseMainCategory();
            patientRecordCaseMainCategory.setRecordCaseBigName(recordCaseBigVo.getRecordCaseBigName());
            patientRecordCaseMainCategory.setRecordCaseId(patientRecordcase.getRecordcaseId());
            int insertPatientRecordCaseMainCategory = patientRecordCaseMainCategoryDao.insertPatientRecordCaseMainCategory(patientRecordCaseMainCategory);
            if (insertPatientRecordCaseMainCategory <= 0){
                throw new Exception("新增患者病例大分类失败");
            }
            //添加病例中分类信息
            if (recordCaseBigVo.getRecordCaseMiddleVoList() == null || recordCaseBigVo.getRecordCaseMiddleVoList().size() <= 0){
                throw new Exception("病例中分类不能为空");
            }
            for (RecordCaseMiddleVo recordCaseMiddleVo : recordCaseBigVo.getRecordCaseMiddleVoList()) {
                PatientRecordCaseMiddleClass patientRecordCaseMiddleClass = new PatientRecordCaseMiddleClass();
                patientRecordCaseMiddleClass.setRecordCaseMiddleName(recordCaseMiddleVo.getRecordCaseDetail());
                patientRecordCaseMiddleClass.setRecordCaseBigId(patientRecordCaseMainCategory.getRecordCaseBigId());
                int inertPatientRecordCaseMiddleClass = patientRecordCaseMiddleClassDao.inertPatientRecordCaseMiddleClass(patientRecordCaseMiddleClass);
                if (inertPatientRecordCaseMiddleClass <= 0){
                    throw new Exception("新增患者病历中分类失败");
                }
                //添加病例明细信息
                if (recordCaseMiddleVo.getRecordCasVoList() == null || recordCaseMiddleVo.getRecordCasVoList().size() <= 0){
                    throw new Exception("病例明细不能为空");
                }
                for (RecordCasVo recordCasVo : recordCaseMiddleVo.getRecordCasVoList()) {
                    PatientMedicalRecords patientMedicalRecords = new PatientMedicalRecords();
                    patientMedicalRecords.setRecordCaseDetailName(recordCasVo.getRecordCasVoName());
                    patientMedicalRecords.setRecordCaseMiddleId(patientRecordCaseMiddleClass.getRecordCaseMiddleId());
                    if (recordCasVo.getRecordCaseDetailContent() != null && !recordCasVo.getRecordCaseDetailContent().equals("")){
                        patientMedicalRecords.setRecordCaseDetailContent(recordCasVo.getRecordCaseDetailContent());
                    }
                    else if (recordCasVo.getRecordCaseValue() != 0){
                        patientMedicalRecords.setRecordCaseValue(recordCasVo.getRecordCaseValue());
                    }
                    else if (recordCasVo.getReportDoctorName() != null && !recordCasVo.getReportDoctorName().equals("")){
                        Doctor doctor = doctorDao.selectDoctorByDoctorName(recordCasVo.getReportDoctorName());
                        if (doctor != null){
                            patientMedicalRecords.setReportDoctorId(doctor.getDoctorId());
                            patientMedicalRecords.setReportDoctorName(recordCasVo.getReportDoctorName());
                        }
                        else {
                            throw new Exception("未查到报告医生信息");
                        }
                    }
                    else if (recordCasVo.getAuditDoctorName() != null && !recordCasVo.getAuditDoctorName().equals("")){
                        Doctor doctor = doctorDao.selectDoctorByDoctorName(recordCasVo.getAuditDoctorName());
                        if (doctor != null){
                            patientMedicalRecords.setAuditDoctorId(doctor.getDoctorId());
                            patientMedicalRecords.setAuditDoctorName(recordCasVo.getAuditDoctorName());
                        }
                        else {
                            throw new Exception("未查到审核医生");
                        }
                    }
                    else if (recordCasVo.getExaminationitemId() != null && recordCasVo.getExaminationitemId() != 0){
                        patientMedicalRecords.setExaminationItemId(recordCasVo.getExaminationitemId());
                    }
                    patientMedicalRecords.setRemark(recordCaseBigVo.getRemark());
                    patientMedicalRecords.setFamilyHistory(recordCaseBigVo.getFamilyHistory());
                    i = patientMedicalRecordsDao.insertPatientMedicalRecords(patientMedicalRecords);
                    if (i <= 0){
                        throw new Exception("新增患者明细信息失败");
                    }
                }
            }
        }
        return i;
    }
}